package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sl.vo.UserVO;
import org.springframework.stereotype.Service;
import com.sl.mapper.UserMapper;
import com.sl.pojo.User;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    //查：所有员工。显示
    public  List<User> findAll() {
        List<User> users = userMapper.selectAll();
        return users;
    }
    //显示所有的在职员工
    public  List<User> findByDeleted(Integer deleted) {
        if(StringUtils.isEmpty(deleted)){
            //无值全显示
            List<User> userList = userMapper.selectAll();
            return userList;
        }
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        //有值 1 离职。。0默认在职
        criteria.andEqualTo("deleted", deleted);
        List<User> users = userMapper.selectByExample(example);

        return users;
    }

    public User findById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public User findByName(String name) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        User user = userMapper.selectOneByExample(example);
        return user;
    }

    //用户名-->users
    public User findByUsername(String username) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andEqualTo("username", username);
        }
        User user = userMapper.selectOneByExample(example);
        return user;
    }

    /**
     * 增加用户。并且把密码加盐
     */
    public void add(User user) {
        if (user.getId() != null) {
            throw new RuntimeException("不能添加ID"); }
        /*//获取盐值
        String gensalt = BCrypt.gensalt();
        //加密明文密码
        String hashpw = BCrypt.hashpw(user.getPassword(), gensalt);
        user.setPassword(hashpw);*/
        userMapper.insert(user);
    }

    /**
     * 修改  .逻辑删除。同
     */
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }


    /**
     * 真实删除
     */
    public void deleteReal(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    public Page<User> findList(Map<String,String> searchMap,Integer page,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
        List<User> userList = userMapper.selectByExample(example);
        return (Page<User>)userList;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, String> searchMap){
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            //
            if(searchMap.get("username")!=null ){
                criteria.andEqualTo("username",searchMap.get("username"));
            }

            if(searchMap.get("name")!=null ){
                criteria.andEqualTo("name",searchMap.get("name"));
            }
            //创建时间
            if(searchMap.get("lastLoginTime")!=null ){
                criteria.andLike("lastLoginTime","%"+searchMap.get("lastLoginTime") +"%");
            }
            //
            if(searchMap.get("deleted")!=null ){
                criteria.andEqualTo("deleted",searchMap.get("deleted"));
            }
        }
        return example;
    }
}